package com.mcflies.aotoPartsManager.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;

import com.mcflies.aotoPartsManager.dao.AutoPartsBaseDAO;
import com.mcflies.aotoPartsManager.dao.CustomerAreaDAO;
import com.mcflies.aotoPartsManager.pojo.CustomerArea;

public class CustomerAreaDAOImpl extends AutoPartsBaseDAO implements CustomerAreaDAO {
	private Logger log = Logger.getLogger(CustomerAreaDAOImpl.class);

	@Override
	public CustomerArea addCustomerArea(CustomerArea area) throws Exception {
		try {
			Session session = this.openDBSession();
			session.save(area);
			return area;
		} catch (Exception e) {
			log.info("在[" + CustomerAreaDAOImpl.class.getName() + "]执行方法(addCustomerArea)发生：：" + e.getMessage() + "异常");
			throw new Exception(e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public Long countCustomerArea() throws Exception {
		try {
			Session session = this.openDBSession();
			String hql = "select count(*) from CustomerArea";
			Query query = session.createQuery(hql);
			List list = query.list();
			Long count = (Long) list.get(0);
			return count;
		} catch (Exception e) {
			log.info("在[" + CustomerAreaDAOImpl.class.getName() + "]执行方法(countCustomerArea)发生：：" + e.getMessage()
					+ "异常");
			throw new Exception(CustomerAreaDAOImpl.class.getName() + "：：查询[客户地域]总数出错");
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CustomerArea> loadCustomerArea(int start, int limit) throws Exception {
		List<CustomerArea> list = new ArrayList<CustomerArea>();
		try {
			Session session = this.openDBSession();
			String hql = "from CustomerArea";
			Query query = session.createQuery(hql);
			query.setFirstResult(start);
			query.setMaxResults(limit);
			list = query.list();
			return list;
		} catch (Exception e) {
			log
					.info("在[" + CustomerAreaDAOImpl.class.getName() + "]执行方法(loadCustomerArea)发生：：" + e.getMessage()
							+ "异常");
			throw new Exception(CustomerAreaDAOImpl.class.getName() + "：：加载[客户地域]信息出错");
		}
	}
}
